package com.java110.mall.system.export.adapt;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.dto.data.ExportDataDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.intf.cart.IStoreOrderCartInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderInnerServiceSMO;
import com.java110.mall.system.export.IExportDataAdapt;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.utils.util.DateUtil;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import com.java110.vo.ResultVo;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service("storeOrderCart")
public class ExportStoreOrderCartAdapt implements IExportDataAdapt {

    private static final int MAX_ROW = 60000;

    @Autowired
    private IStoreOrderInnerServiceSMO storeOrderInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderCartInnerServiceSMO storeOrderCartInnerServiceSMOImpl;


    @Override
    public SXSSFWorkbook exportData(ExportDataDto exportDataDto) {
        SXSSFWorkbook workbook = null;  //工作簿
        //工作表
        workbook = new SXSSFWorkbook();
        workbook.setCompressTempFiles(false);

        Sheet sheet = workbook.createSheet("订单信息表");
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("订单号");
        row.createCell(1).setCellValue("商品名称");
        row.createCell(2).setCellValue("购买数量");
        row.createCell(3).setCellValue("平台");
        row.createCell(4).setCellValue("类型");
        row.createCell(5).setCellValue("用户名称");
        row.createCell(6).setCellValue("支付金额");
        row.createCell(7).setCellValue("优惠金额");
        row.createCell(8).setCellValue("订单状态");
        row.createCell(9).setCellValue("创建时间");

        JSONObject reqJson = exportDataDto.getReqJson();
        StoreOrderDto storeOrderDto = BeanConvertUtil.covertBean(reqJson, StoreOrderDto.class);

        //查询数据
        getReprotData(sheet, storeOrderDto);

        return workbook;
    }

    private void getReprotData(Sheet sheet, StoreOrderDto storeOrderDto) {
        storeOrderDto.setPage(1);
        storeOrderDto.setRow(MAX_ROW);
        List<StoreOrderDto> storeOrderDtos = storeOrderInnerServiceSMOImpl.queryStoreOrders(storeOrderDto);
        // 查询订单 商品
        queryStoreOrderCarts(storeOrderDtos);

        setData(storeOrderDtos, sheet, 0);
    }

    private void setData(List<StoreOrderDto> storeOrderDtos, Sheet sheet, int step) {
        Row row = null;
        StoreOrderDto storeOrderDto = null;
        for (int rowIndex = 0; rowIndex < storeOrderDtos.size(); rowIndex++) {
            row = sheet.createRow(rowIndex + step + 1);
            storeOrderDto = storeOrderDtos.get(rowIndex);
            row.createCell(0).setCellValue(storeOrderDto.getOrderId());
            row.createCell(1).setCellValue(storeOrderDto.getStoreOrderCartDtos().stream().map(StoreOrderCartDto::getProdName).collect(Collectors.joining("、")));
            row.createCell(2).setCellValue(storeOrderDto.getStoreOrderCartDtos().stream().map(StoreOrderCartDto::getCartNum).collect(Collectors.joining("、")));
            row.createCell(3).setCellValue(storeOrderDto.getAppName());
            row.createCell(4).setCellValue("3307".equals(storeOrderDto.getCartType()) ? "服务": "商品");
            row.createCell(5).setCellValue(storeOrderDto.getPersonName());
            row.createCell(6).setCellValue(storeOrderDto.getPayPrice());
            row.createCell(7).setCellValue(StringUtil.isEmpty(storeOrderDto.getDeduction())? "0": storeOrderDto.getDeduction() );
            row.createCell(8).setCellValue(storeOrderDto.getStateName());
            row.createCell(9).setCellValue(DateUtil.getFormatTimeStringA(storeOrderDto.getCreateTime()));
        }
    }

    /**
     * 查询订单商品
     * @param storeOrderDtos
     */
    private void queryStoreOrderCarts(List<StoreOrderDto> storeOrderDtos) {
        if(ListUtil.isNull(storeOrderDtos)){
            return;
        }

        List<String> orderIds = new ArrayList<>();
        for(StoreOrderDto storeOrderDto: storeOrderDtos){
            orderIds.add(storeOrderDto.getOrderId());
        }

        StoreOrderCartDto storeOrderCartDto = new StoreOrderCartDto();
        storeOrderCartDto.setOrderIds(orderIds.toArray(new String[orderIds.size()]));
        List<StoreOrderCartDto> storeOrderCartDtos =
                storeOrderCartInnerServiceSMOImpl.queryStoreOrderCarts(storeOrderCartDto);
        if(ListUtil.isNull(storeOrderCartDtos)){
            return;
        }
        List<StoreOrderCartDto> tmpCarts = null;
        for(StoreOrderDto storeOrderDto: storeOrderDtos) {
            tmpCarts = new ArrayList<>();
            for (StoreOrderCartDto cartDto : storeOrderCartDtos) {
                if (!storeOrderDto.getOrderId().equals(cartDto.getOrderId())){
                    continue;
                }
                storeOrderDto.setCartType(cartDto.getCartType());
                tmpCarts.add(cartDto);
            }
            storeOrderDto.setStoreOrderCartDtos(tmpCarts);
        }
    }

}
